package com.DAO;

import com.Module.UserDelete;
import com.Util.Sql;
import com.Module.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Userinfo implements Sql {
    public List<User> list=new ArrayList<>();
    @Override
    public void select() {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            String sql="select  * from vipinfo";
            ResultSet resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                int id=resultSet.getInt("v_id");
                String name=resultSet.getString("v_name");
                String password=resultSet.getString("v_pwd");
                String tel=resultSet.getString("v_tel");
                User user=new User(id,name,password,tel);
                list.add(user);
            }
            databaseConnection.release(connection,statement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Object> select(String idinfo) {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        List<Object> list1=new ArrayList<>();
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            String sql="select  * from vipinfo where v_id="+idinfo;
            ResultSet resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                int id=resultSet.getInt("v_id");
                String name=resultSet.getString("v_name");
                String password=resultSet.getString("v_pwd");
                String tel=resultSet.getString("v_tel");
                User user=new User(id,name,password,tel);
                Object ans=(Object) user;
                list1.add(ans);
            }
            databaseConnection.release(connection,statement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  list1;
    }

    @Override
    public int update() {
        return 0;
    }

    @Override
    public int update(Object obj) {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        int ans = 0;
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            User user=(User)   obj;
            String sql="INSERT INTO vipinfo VALUES("+ user.getId()+",+'"+ user.getPassword()+"','"+ user.getName()+"','"+user.getTel() +"')";
//            INSERT INTO vipinfo VALUES(7,"123456","王丽","18485808609")
            ans=statement.executeUpdate(sql);
            databaseConnection.release(connection,statement);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ans;
    }

    @Override
    public int delete(Object obj) {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        int ans = 0;
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            UserDelete userDelete=(UserDelete) obj;
            String sql="DELETE FROM vipinfo WHERE v_id="+userDelete.getId();
            ans=statement.executeUpdate(sql);
            databaseConnection.release(connection,statement);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }



    @Override
    public int modify(Object obj) {
        return 0;
    }
}
